<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/7/19
 * Time: 下午2:05
 */

namespace App\Http\Model;


use Illuminate\Support\Facades\Redis;


class RoomHeart
{
    private $redisRoomHeartSelectMap = "ROOM:HEART:SELECT:"; //ROOM:HEART::{room_id}
    private $redisRoomHeartMap = "ROOM:HEART:"; //ROOM:HEART:STATE:{room_id}

    public function clean($roomId)
    {
        $stateKey = $this->redisRoomHeartSelectMap . $roomId;
        Redis::DEL($stateKey);
        $key = $this->redisRoomHeartMap . $roomId;
        return Redis::DEL($key);
    }

    /**
     * 设置心动状态 0.未开启 1.开启 2.选择 3.公布
     * @param $roomId
     * @param $state
     * @return boolean
     */
    public function setState($roomId, $state)
    {
        if (empty($roomId)) {
            return false;
        }
        $key = $this->redisRoomHeartMap . $roomId;
        $ret = Redis::HSET($key, 'state', $state);
        //Redis::EXPIRE($key, 3600);
        return $ret;
    }


    /**
     * 获取心动状态 0.未开启 1.开启 2.选择 3.公布
     * @param $roomId
     * @return int
     */
    public function getState($roomId)
    {
        $key = $this->redisRoomHeartMap . $roomId;
        $ret = Redis::HGET($key, 'state');
        if (empty($ret)) {
            return 0;
        }
        return intval($ret);
    }


    /**
     * 设置心动选择
     * @param $roomId
     * @param $userId
     * @param $selectUserId
     * @return bool
     */
    public function setSelect($roomId, $userId, $selectUserId)
    {
        if (empty($roomId) || empty($userId) || empty($selectUserId)) {
            return false;
        }
        $key = $this->redisRoomHeartSelectMap . $roomId;
        return Redis::HSETNX($key, $userId, $selectUserId);
    }


    /**
     * 获取心动选择
     * @param $roomId
     * @param $userId
     * @return array
     */
    public function getSelect($roomId, $userId = null)
    {
        $retData = [];
        $key = $this->redisRoomHeartSelectMap . $roomId;
        if (!is_null($userId)){
            $val = Redis::HGET($key, $userId);
            $retData[] = [
                'user_id' => $userId,
                'select_user_id' => $val
            ];
        }
        $retArr = Redis::HGETALL($key);
        foreach ($retArr as $k => $v){
            $retData [] = [
                'user_id' => $k,
                'select_user_id' =>$v
            ];
        }
        return $retData;
    }


    /**
     * 设置开启时间
     * @param $roomId
     * @param $time
     * @return bool
     */
    public function setSelectTime($roomId, $time)
    {
        if (empty($roomId)) {
            return false;
        }
        $key = $this->redisRoomHeartMap . $roomId;
        return Redis::HSET($key, 'select_time', $time);
    }


    /**
     * 获取开启时间
     * @param $roomId
     * @return bool
     */
    public function getSelectTime($roomId)
    {
        if (empty($roomId)) {
            return false;
        }
        $key = $this->redisRoomHeartMap . $roomId;
        $ret = Redis::HGET($key, 'select_time');
        if (empty($ret)){
            return 0;
        }
        return intval($ret);
    }

}